~ chicken-core (master) /manual/Module (chicken memory representation)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken memory representation)
  5
  6The procedures from this module operate on the in-memory
  7representation of Scheme objects.  These procedures ''are'' safe, so,
  8for example, unlike the procedures from {{(chicken memory)}} these
  9procedures ''will'' type-check and range-check their arguments, but
 10you still need to know what you're doing because the effects may be
 11surprising for the uninitiated.
 12
 13
 14=== Extending procedures with data
 15
 16
 17==== extend-procedure
 18
 19<procedure>(extend-procedure PROCEDURE X)</procedure>
 20
 21Returns a copy of the procedure {{PROCEDURE}} which contains an
 22additional data slot initialized to {{X}}. If {{PROCEDURE}} is already
 23an extended procedure, then its data slot is changed to contain {{X}}
 24and the same procedure is returned. Signals an error when
 25{{PROCEDURE}} is not a procedure.
 26
 27
 28==== extended-procedure?
 29
 30<procedure>(extended-procedure? PROCEDURE)</procedure>
 31
 32Returns {{#t}} if {{PROCEDURE}} is an extended procedure,
 33or {{#f}} otherwise.
 34
 35
 36==== procedure-data
 37
 38<procedure>(procedure-data PROCEDURE)</procedure>
 39
 40Returns the data object contained in the extended procedure
 41{{PROCEDURE}}, or {{#f}} if it is not an extended procedure.
 42
 43
 44==== set-procedure-data!
 45
 46<procedure>(set-procedure-data! PROCEDURE X)</procedure>
 47
 48Changes the data object contained in the extended procedure
 49{{PROCEDURE}} to {{X}}. Signals an error when {{PROCEDURE}} is not an
 50extended procedure.
 51
 52<enscript highlight=scheme>
 53(define foo
 54  (letrec ((f (lambda () (procedure-data x)))
 55           (x #f) )
 56    (set! x (extend-procedure f 123))
 57    x) )
 58(foo)                                         ==> 123
 59(set-procedure-data! foo 'hello)
 60(foo)                                         ==> hello
 61</enscript>
 62
 63
 64
 65=== Low-level data access
 66
 67These procedures operate with what are known as ''vector-like
 68objects''. A ''vector-like object'' is a vector, record structure,
 69pair, symbol or keyword: it is an aggregation of other Scheme objects.
 70
 71Note that strings and bytevectors are not considered vector-like (they are
 72considered to be ''byte vectors'', which are objects of mostly
 73unstructured binary data).
 74
 75
 76==== vector-like?
 77
 78<procedure>(vector-like? X)</procedure>
 79
 80Returns {{#t}} when {{X}} is a vector-like object, returns {{#f}}
 81otherwise.
 82
 83
 84==== block-ref
 85
 86<procedure>(block-ref VECTOR* INDEX)</procedure>
 87
 88Returns the contents of the {{INDEX}}th slot of the vector-like object
 89{{VECTOR*}}.
 90
 91
 92==== block-set!
 93
 94<procedure>(block-set! VECTOR* INDEX X)</procedure><br>
 95<procedure>(set! (block-ref VECTOR* INDEX) X)</procedure>
 96
 97Sets the contents of the {{INDEX}}th slot of the vector-like object
 98{{VECTOR*}} to the value of {{X}}.
 99
100==== number-of-slots
101
102<procedure>(number-of-slots VECTOR*)</procedure>
103
104Returns the number of slots that the vector-like object {{VECTOR*}}
105contains.
106
107
108==== number-of-bytes
109
110<procedure>(number-of-bytes BLOCK)</procedure>
111
112Returns the number of bytes that the object {{BLOCK}}
113holds. {{BLOCK}} may be any non-immediate value. For strings and symbols, the size
114of the buffer holding the encoded characters is returned.
115
116
117==== object-copy
118
119<procedure>(object-copy X)</procedure>
120
121Copies {{X}} recursively and returns the fresh copy. Objects allocated
122in static memory are copied back into garbage collected storage.
123
124
125==== number-vector-data
126
127<procedure>(number-vector-data VECTOR)</procedure>
128
129Returns the bytevector holding the raw data of the numeric vector {{VECTOR}},
130which should be a bytevector or a homogenous number vector as those
131exposed by the {{(chicken numvector)}} and {{srfi-4}} library modules.
132If {{VECTOR}} is a bytevector, the result will be identical to the argument.
133The returned bytevector shares storage with the original value.
134
135
136=== Record instance
137
138
139==== make-record-instance
140
141<procedure>(make-record-instance SYMBOL ARG1 ...)</procedure>
142
143Returns a new instance of the record type {{SYMBOL}}, with its
144slots initialized to {{ARG1 ...}}.  To illustrate:
145
146<enscript highlight=scheme>
147(define-record-type point (make-point x y) point?
148  (x point-x point-x-set!)
149  (y point-y point-y-set!))
150</enscript>
151
152expands into something quite similar to:
153
154<enscript highlight=scheme>
155(begin
156  (define (make-point x y)
157    (make-record-instance 'point x y) )
158  (define (point? x)
159    (and (record-instance? x)
160         (eq? 'point (block-ref x 0)) ) )
161  (define (point-x p) (block-ref p 1))
162  (define (point-x-set! p x) (block-set! p 1 x))
163  (define (point-y p) (block-ref p 2))
164  (define (point-y-set! p y) (block-set! p 1 y)) )
165</enscript>
166
167
168==== record-instance?
169
170<procedure>(record-instance? X [SYMBOL])</procedure>
171
172Returns {{#t}} if {{X}} is a record structure, or {{#f}} otherwise.
173
174Further, returns {{#t}} if {{X}} is of type {{SYMBOL}}, or {{#f}}
175otherwise.
176
177
178==== record-instance-type
179
180<procedure>(record-instance-type RECORD)</procedure>
181
182Returns type symbol of the record structure {{RECORD}}. Signals an
183error if {{RECORD}} is not a record structure.
184
185
186==== record-instance-length
187
188<procedure>(record-instance-length RECORD)</procedure>
189
190Returns number of slots for the record structure {{RECORD}}. The
191record-instance type is not counted. Signals an error if
192{{RECORD}} is not a record structure.
193
194
195==== record-instance-slot
196
197<procedure>(record-instance-slot RECORD INDEX)</procedure>
198
199Returns the contents of the {{INDEX}}th slot of the record structure
200{{RECORD}}. The slot index range is the open interval {{[0
201record-instance-length)}}. Signals an error if {{RECORD}} is not a record
202structure.
203
204
205==== record-instance-slot-set!
206
207<procedure>(record-instance-slot-set! RECORD INDEX X)</procedure><br>
208<procedure>(set! (record-instance-slot RECORD INDEX) X)</procedure>
209
210Sets the {{INDEX}}th slot of the record structure {{RECORD}} to
211{{X}}. The slot index range is the open interval {{[0
212record-instance-length)}}. Signals an error if {{RECORD}} is not a
213record structure.
214
215
216==== record->vector
217
218<procedure>(record->vector RECORD)</procedure>
219
220Returns a new vector with the type and the elements of the record
221structure {{RECORD}}. Signals an error if {{RECORD}} is not a record
222structure.
223
224
225=== Magic
226
227
228==== object-become!
229
230<procedure>(object-become! ALIST)</procedure>
231
232Changes the identity of the value of the car of each pair in {{ALIST}}
233to the value of the cdr. Neither value may be immediate (i.e. exact
234integers, characters, booleans or the empty list).
235
236<enscript highlight=scheme>
237(define x "i used to be a string")
238(define y '#(and now i am a vector))
239(object-become! (list (cons x y)))
240x                                    ==> #(and now i am a vector)
241y                                    ==> #(and now i am a vector)
242(eq? x y)                            ==> #t
243</enscript>
244
245Note: this operation invokes a major garbage collection.
246
247The effect of using {{object-become!}} on evicted data (see
248{{object-evict}}) is undefined.
249
250
251==== mutate-procedure!
252
253<procedure>(mutate-procedure! OLD PROC)</procedure>
254
255Replaces the procedure {{OLD}} with the result of calling the
256one-argument procedure {{PROC}}. {{PROC}} will receive a copy of
257{{OLD}} that will be identical in behaviour to the result of {{OLD}}:
258
259<enscript highlight=scheme>
260 ;;; Replace arbitrary procedure with tracing one:
261
262 (mutate-procedure! my-proc
263   (lambda (new)
264     (lambda args
265       (printf "~s called with arguments: ~s~%" new args)
266       (apply new args) ) ) )
267</enscript>
268
269
270---
271Previous: [[Module (chicken memory)]]
272
273Next: [[Module (chicken module)]]
Trap